import * as remote from '@electron/remote';

import { IOperationBrowserService } from 'egret/platform/operations/common/operations-browser';
import { IFocusablePart, KeybindingType } from 'egret/platform/operations/common/operations';
import { globalObserver, GLOBAL_EVENT } from 'game/observer';

/**
 *
 */
export enum GameCommands {
	REFRESH = "refresh",
	FULL_SCREEN = "full_screen",
	DEV_TOOLS = "dev_tools",
}

/**
 * 工作台
 */
export class GameWorkbench implements IFocusablePart {

	private parent: HTMLElement;
	private GAME_CMDS: { [cmd: string]: Function } = {};

	constructor(
		parent: HTMLElement,
		@IOperationBrowserService private operationService: IOperationBrowserService,
	) {
		this.parent = parent;
		this.parent.tabIndex = 0;

		this.operationService.registerFocusablePart(this);
		this.operationService.registerKeybingding(GameCommands.REFRESH, 'f5', KeybindingType.KEY_DOWN, "", "", false);
		this.operationService.registerKeybingding(GameCommands.FULL_SCREEN, 'f11', KeybindingType.KEY_DOWN, "", "", false);
		this.operationService.registerKeybingding(GameCommands.DEV_TOOLS, 'f12', KeybindingType.KEY_DOWN, "", "", false);

		globalObserver.event(GLOBAL_EVENT.on_game_cmd_upd, (...arg) => {
			this.updGameCmds();
		});
		this.updGameCmds();
	}

	/**
	 * 安装
	 */
	public startup(): void {
		this.initRCMenu();
	}

	private initRCMenu(): void {

		window.addEventListener("contextmenu", (e) => {
			e.preventDefault();
			const menuInfo = [
				{
					label: "全屏展示",
					accelerator: "f11",
					click: this.fullScreen.bind(this)
				},
				{
					label: "刷新",
					accelerator: "f5",
					click: this.refresh.bind(this)
				},
				{
					label: "调试",
					accelerator: "f12",
					click: this.openDevTools.bind(this)
				}
			];

			let curWindows = remote.getCurrentWindow();
			if (curWindows.isFullScreen()) {
				menuInfo[0].label = "退出全屏";
			} else {
				menuInfo[0].label = "全屏展示";
			}
			let gameMenu = remote.Menu.buildFromTemplate(menuInfo);

			gameMenu.popup({ window: curWindows });
		});
	}

	public getRelativeELement(): HTMLElement {
		return document.body;
	}

	public executeCommand<T>(command: string, ...args): Promise<T> {
		if (this.GAME_CMDS[command]) {
			this.GAME_CMDS[command](...args);
			return Promise.resolve(void 0);
		}
		switch (command) {
			case GameCommands.FULL_SCREEN:
				this.fullScreen();
				break;
			case GameCommands.REFRESH:
				this.refresh();
				break;
			case GameCommands.DEV_TOOLS:
				this.openDevTools();
				break;
			default:
				break;
		}
		return Promise.resolve(void 0);
	}

	public hasCommand(command: string): boolean {
		if (this.GAME_CMDS[command]) {
			return true;
		}
		return [
			GameCommands.FULL_SCREEN,
			GameCommands.REFRESH,
			GameCommands.DEV_TOOLS
		].indexOf(command as GameCommands) != -1;
	}

	private updGameCmds(): void {
		if ((<any>document).GAME_CMDS) {
			this.GAME_CMDS = {};
			for (const key in (<any>document).GAME_CMDS) {
				this.GAME_CMDS[`game_cmd_${key}`] = (<any>document).GAME_CMDS[key];
				this.operationService.registerKeybingding(`game_cmd_${key}`, key, KeybindingType.KEY_DOWN, "", "", false);
			}
		}
	}

	private fullScreen(): void {
		let curWindows = remote.getCurrentWindow();
		if (curWindows.isFullScreen()) {
			curWindows.setFullScreen(false);
		} else {
			curWindows.setFullScreen(true);
		}
	}

	private refresh(): void {
		let curWindows = remote.getCurrentWindow();
		curWindows.reload();
	}

	private openDevTools(): void {
		let curWindows = remote.getCurrentWindow();
		curWindows.webContents.openDevTools();
	}
}
